/*
 * Copyright (C) 2015-2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#ifndef PLANET_WARS_2D_RT_CORE_MODEL_PLAYER_H
#define PLANET_WARS_2D_RT_CORE_MODEL_PLAYER_H


#include "PlanetWars2dRT-core/utils/color_rgb_essential.h"
#include "PlanetWars2dRT-core/specific/model/game_essential.h"


#define PLAYER_NAME_LENGTH_MAX  50
#define PLAYER_NEUTRAL_COLOR_RED_PLANET   120
#define PLAYER_NEUTRAL_COLOR_GREEN_PLANET 120
#define PLAYER_NEUTRAL_COLOR_BLUE_PLANET  120
#define PLAYER_NEUTRAL_COLOR_RED_SHIP      60
#define PLAYER_NEUTRAL_COLOR_GREEN_SHIP    60
#define PLAYER_NEUTRAL_COLOR_BLUE_SHIP     60


/* useful because of include loop with game */

#ifndef PLANET_WARS_2D_RT_CORE_MODEL_PLAYER_TYPEDEF
#define PLANET_WARS_2D_RT_CORE_MODEL_PLAYER_TYPEDEF 1
typedef struct player_t player_t;
#endif

struct game_t;


typedef void (*player_act_t)(struct game_t*   current_game,
			     struct player_t* this_player);

struct player_t
{
  char* name;
  color_rgb color;
  
  void* data;
  void (*data_free)(void* data);
  player_act_t act;
  /* AI must only view the game and give order to the items of a player. */
  /* But with this implementation, AI can do far more... */
  /* TODO Implement the command or proxy pattern */
};


EXTERN_C_BEGIN


void
player_init_first_time_only(player_t* a_player);

const char*
player_get_name(const player_t* a_player);

bool
player_set_name(player_t* a_player, const char* new_name);

static inline
color_rgb
player_get_color_rgb_planet(const player_t* a_player)
{
  return a_player->color;
}

static inline
color_rgb
player_get_color_rgb_ship(const player_t* a_player)
{
  color_rgb color = a_player->color;
  color.red   /= (unsigned char) 2;
  color.green /= (unsigned char) 2;
  color.blue  /= (unsigned char) 2;
  return color;
}

void
player_act_unsafe(player_t* a_player, struct game_t* a_game);

void
player_act(player_t* a_player, struct game_t* a_game);

void
player_destructor(player_t* a_player);


EXTERN_C_END


#endif
